﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace FrameworkLib.Configuration
{
    [Serializable]
    public class FLibConfigurationSection<T> : ConfigurationSection where T : ConfigurationSection
    {
        //public static string SectionGroupName = "ImporterSection";

        public FLibConfigurationSection()
        {
        }

        public static T GetSection(string sectionName)
        {
            return ConfigurationManager.GetSection(sectionName) as T;
        }

        public static T GetSection(string sectionGroupName, string sectionName)
        {
            string s = string.Format(@"{0}/{1}", sectionGroupName, sectionName);

            return ConfigurationManager.GetSection(s) as T;
        }

        //public static T GetSection()
        //{
        //    string sectionName = string.Empty;

        //    System.Reflection.MemberInfo info = typeof(FLibConfigurationSection<T>);
        //    object[] attributes = info.GetCustomAttributes(true);
        //    for (int i = 0; i < attributes.Length; i++)
        //    {
        //        if(attributes[i] is ConfigurationSectionNameAttribute)
        //        {
        //            sectionName = ((ConfigurationSectionNameAttribute)attributes[i]).SectionName;
        //            break;
        //        }
        //    }

        //    //
        //    return ConfigurationManager.GetSection(string.Format(@"{0}/{1}", SectionGroupName, sectionName)) as T;
        //}
    }

    [AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
    public sealed class ConfigurationSectionNameAttribute : Attribute
    {
        readonly string m_name;
        
        // This is a positional argument
        public ConfigurationSectionNameAttribute(string sectionName)
        {
            this.m_name = sectionName;
        }

        public string SectionName
        {
            get { return m_name; }
        }
    }
}
